package com.beta.restapi.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.beta.entity.Shipments;
import com.beta.restapi.internal.Consts;
import com.beta.common.web.ContentType;
import com.beta.dto.Shipment.ShipmentsDetailDto;
import com.beta.dto.Shipment.ShipmentsDto;
import com.beta.em.order.ShipmentStatus;
import com.beta.vo.ShipmentsDetailVO;
import com.beta.vo.ShipmentsPageVO;
import com.beta.service.IShipmentsService;
import io.swagger.annotations.Api;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lichao on 2016/11/28.
 */
@Api(tags = {"Shipments"})
@RestController
@RequestMapping(Consts.API_PATH_PREFIX + Consts.Resource.SHIPMENTS)
public class ShipmentsController extends BaseController {

    @Autowired
    private IShipmentsService shipmentsService;
    @Autowired
    private MapperFacade beanMapper;

    @GetMapping(value = "", produces = {ContentType.JSON})
    public Object getShipments(@RequestHeader(Consts.HEADER_AUTH_KEY) String token,
                               @RequestParam(value = "q[status]", required = false, defaultValue = "") String status) {
        Page<ShipmentsDto> pageResult = null;

        pageResult = shipmentsService.getShipments(getCurrentUserId(), getMybatisPage(), status);
        ShipmentsPageVO shipmentsVO = new ShipmentsPageVO();
        shipmentsVO.setShipments(pageResult.getRecords());
        fillPageParam(pageResult, shipmentsVO);
        return ok(shipmentsVO);
    }

    @GetMapping(value = "/{shipmentsId}/details", produces = {ContentType.JSON})
    public Object getShipmentsDetails(@RequestHeader(Consts.HEADER_AUTH_KEY) String token,
                                      @PathVariable("shipmentsId") Integer shipmentsId) {
        ShipmentsDetailDto shipmentsDetailDto = shipmentsService.getShipmentsDetail(shipmentsId);
        ShipmentsDetailVO shipmentsDetailVO = beanMapper.map(shipmentsDetailDto, ShipmentsDetailVO.class);
        return ok(shipmentsDetailVO);
    }

    @PatchMapping(value = "/{shipmentsId}/assignment_confirm", produces = {ContentType.JSON})
    public Object confirmAssignment(@RequestHeader(Consts.HEADER_AUTH_KEY) String token,
                                    @PathVariable("shipmentsId") Integer shipmentsId) {
        Shipments shipment = shipmentsService.executeConfirmAssignment(shipmentsId);
        Map<String, String> map = new HashMap<>();
        map.put("human_status_name", ShipmentStatus.getCnValue(shipment.getStatus()));
        return ok(map);
    }

    @PatchMapping(value = "/{shipmentsId}/ship", produces = {ContentType.JSON})
    public Object confirmShip(@RequestHeader(Consts.HEADER_AUTH_KEY) String token,
                              @PathVariable("shipmentsId") Integer shipmentsId) {
        Shipments shipment = shipmentsService.executeConfirmShip(shipmentsId);
        Map<String, String> map = new HashMap<>();
        map.put("human_status_name", ShipmentStatus.getCnValue(shipment.getStatus()));
        return ok(map);
    }

    @PatchMapping(value = "{shipmentsId}/receipt_confirm", produces = {ContentType.JSON})
    public Object receiptConfirm(@RequestHeader(Consts.HEADER_AUTH_KEY) String token,
                                 @PathVariable("shipmentsId") Integer shipmentsId) {
        Shipments shipment = shipmentsService.executeReceiptConfirm(shipmentsId);
        Map<String, String> map = new HashMap<>();
        map.put("human_status_name", ShipmentStatus.getCnValue(shipment.getStatus()));
        return ok(map);
    }

}
